package org.absolutezero.client.presenter;

import javax.inject.Inject;

import org.absolutezero.client.events.DeleteRecipeEvent;
import org.absolutezero.client.events.DeleteRecipeEventHandler;
import org.absolutezero.client.events.EditRecipeEvent;
import org.absolutezero.client.events.EditRecipeEventHandler;
import org.absolutezero.client.events.NavigationChangeEvent;
import org.absolutezero.client.events.NavigationChangeEvent.NavigationItemSelected;
import org.absolutezero.client.events.NavigationChangeEventHandler;
import org.absolutezero.client.events.ViewRecipeEvent;
import org.absolutezero.client.events.ViewRecipeEventHandler;
import org.absolutezero.client.util.Dialog;
import org.absolutezero.client.view.RecipesView;
import org.absolutezero.shared.RecipeProxy;
import org.absolutezero.shared.RecipeRequest;
import org.absolutezero.shared.RefrigeratorRequestFactory;

import com.google.gwt.event.shared.EventBus;
import com.google.web.bindery.requestfactory.shared.Receiver;
import com.google.web.bindery.requestfactory.shared.Request;

public class RecipesPresenter {

	private final RecipesView recipes;
	private final EventBus ebus;
	private final RefrigeratorRequestFactory requestFactory;
	
	@Inject
	public RecipesPresenter(RecipesView recipes,
						EventBus ebus,
						RefrigeratorRequestFactory requestFactory) {
		
		this.ebus = ebus;
		this.recipes = recipes;
		this.requestFactory = requestFactory;
		
		bind();
	}
	
	private void bind(){
		ebus.addHandler(NavigationChangeEvent.TYPE, new NavigationChangeEventHandler() {
			
			@Override public void onEvent(NavigationChangeEvent e) { onNavigationChange(e); }
		});
		ebus.addHandler(DeleteRecipeEvent.TYPE, new DeleteRecipeEventHandler() {
			
			@Override public void onEvent(DeleteRecipeEvent e) { onRecipeDelete(e); }
		});
		ebus.addHandler(EditRecipeEvent.TYPE, new EditRecipeEventHandler() {
	
			@Override public void onEvent(EditRecipeEvent e) { onRecipeEdit(e); }
		});
		ebus.addHandler(ViewRecipeEvent.TYPE, new ViewRecipeEventHandler() {
			
			@Override public void onEvent(ViewRecipeEvent e) { onRecipeView(e); }
		});
	}
	
	private void onNavigationChange(NavigationChangeEvent e){
		
		switch(e.getNavigationItemSelected()){
			
		case RECIPE_BROWSE:
			recipes.setBrowseRecipes();
			break;
			
		case RECIPE_MATCH:
			recipes.setMatchRecipe();
			break;
			
		case RECIPE_MY:
			recipes.setMyRecipes();
			break;
		}
	}
	
	private void onRecipeDelete(DeleteRecipeEvent e){
		
		RecipeRequest request = requestFactory.recipeRequest();
		RecipeProxy r = request.edit(e.getRecipe());
		Request<Void> deleteReq = request.remove().using(r);
		
		deleteReq.fire(new Receiver<Void>(){

			@Override
			public void onSuccess(Void response) {
				Dialog.show("Recipe delete", "success");
				ebus.fireEvent(new NavigationChangeEvent(NavigationItemSelected.RECIPE_MY));
			}
			
		});
	}
	
	private void onRecipeEdit(EditRecipeEvent e){
		recipes.setEditRecipe(e.getRecipe());
	}
	
	private void onRecipeView(ViewRecipeEvent e){
		recipes.setViewRecipe(e.getRecipe());
	}
}
